package cn.com.api.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_goods_topic;
import cn.com.api.service.ApiT_goods_topicService;
import cn.com.uthink.util.DateTimeUtil;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "t_goods_topic/")
public class ApiT_goods_topicController {
    @Autowired
    private ApiT_goods_topicService apiT_goods_topicService;
	
	private static Logger logger = Logger.getLogger(ApiT_goods_topicController.class);
	
	
	/**
	 * 获得专场时间
	 * @author jw
	 * 
	 */
	@RequestMapping(value = "getSpecialTimes")
	public void getSpecialTimes(HttpServletRequest request, HttpServletResponse response)throws Exception {
		Map<String,Object> map=new HashMap<>();
		try {
			List<Map<String,Object>> timeMap=apiT_goods_topicService.getTimeTitle();
			map.put("datas", timeMap);
			map.put(Utils.MESSAGE,"查询成功");
			map.put(Utils.RESULT,Utils.RESULT_RC_SUCCESS);
		} catch (Exception e) {
			map.put(Utils.MESSAGE,"查询失败");
			map.put(Utils.RESULT,Utils.RESULT_RC_EXCEPTION);
			e.printStackTrace();
		}
		response.setCharacterEncoding("UTF-8");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertT_goods_topic")
	public void insertT_goods_topic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("topic_id");
		String title = request.getParameter("title");
		String intro = request.getParameter("intro");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String topic_img = request.getParameter("topic_img");
		String member_id = request.getParameter("member_id");
		String status = request.getParameter("status");
		String template_id = request.getParameter("template_id");
		String sum_count = request.getParameter("sum_count");
		String type_id = request.getParameter("type_id");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiT_goods_topic t_goods_topic = new ApiT_goods_topic();
				
    		    //t_goods_topic.setTopic_id(topic_id);
    		    //t_goods_topic.setTitle(title);
    		    //t_goods_topic.setIntro(intro);
    		    //t_goods_topic.setStart_time(start_time);
    		    //t_goods_topic.setEnd_time(end_time);
    		    //t_goods_topic.setTopic_img(topic_img);
    		    //t_goods_topic.setMember_id(member_id);
    		    //t_goods_topic.setStatus(status);
    		    //t_goods_topic.setTemplate_id(template_id);
    		    //t_goods_topic.setSum_count(sum_count);
    		    //t_goods_topic.setType_id(type_id);
    		    //t_goods_topic.setExt_1(ext_1);
    		    //t_goods_topic.setExt_2(ext_2);
    		    //t_goods_topic.setExt_3(ext_3);
    		    //t_goods_topic.setExt_4(ext_4);
    		    //t_goods_topic.setExt_5(ext_5);
				
				apiT_goods_topicService.insertT_goods_topic(t_goods_topic);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_goods_topicById")
	public void findT_goods_topicById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiT_goods_topic t_goods_topic = apiT_goods_topicService.findT_goods_topicById(Integer.valueOf(id));
		
			// return
            map.put("datas", t_goods_topic);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_goods_topics")
	public void findT_goods_topics(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiT_goods_topic> t_goods_topics = apiT_goods_topicService.findT_goods_topics();
		
			// return
            map.put("datas", t_goods_topics);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateT_goods_topic")
	public void updateT_goods_topic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("topic_id");
		String title = request.getParameter("title");
		String intro = request.getParameter("intro");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String topic_img = request.getParameter("topic_img");
		String member_id = request.getParameter("member_id");
		String status = request.getParameter("status");
		String template_id = request.getParameter("template_id");
		String sum_count = request.getParameter("sum_count");
		String type_id = request.getParameter("type_id");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiT_goods_topic t_goods_topic = new ApiT_goods_topic();
				//获取原数据
				t_goods_topic  = apiT_goods_topicService.findT_goods_topicById(Integer.valueOf(id));
				//修改
				//t_goods_topic.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiT_goods_topicService.updateT_goods_topic(t_goods_topic);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteT_goods_topic")
	public void deleteT_goods_topic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiT_goods_topicService.deleteT_goods_topic(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	
	@RequestMapping("/getAllGoodsTopicInfo")
	public void getAllGoodsTopicInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<Map<String,Object>> list = apiT_goods_topicService.getGoodsTopciInfo();
			// return
            map.put("datas", list);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
	/**
	 * 种类和专场
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getAllTypes")
	public void getAllTypes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<Map<String,Object>> list = apiT_goods_topicService.getAllTypes();
            map.put("datas", list);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
	/**
	 * 根据时间和专场类别 获取专场信息
	 * @author JW
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getTopicByCondtion")
	public void getTopicByCondtion(HttpServletRequest request, HttpServletResponse response,
			String typeId, String time, PageInfo page) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<Map<String,Object>> topicListMap= apiT_goods_topicService.getTopicByCondtion(typeId, time,page);
            map.put("datas", topicListMap);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
 
	
	/**
	 * 专场的商品信息
	 * @author JW
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getGoodsInfoByTopicId")
	public void getGoodsInfoByTopicId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			String name = request.getParameter("name");
			String currentPage=request.getParameter("currentPage");
			String topicId=request.getParameter("topicId");
			int curr=Integer.parseInt(currentPage)-1;
			int from=Util.DEFAULT_PAGE_COUNT*curr;
			int to=Util.DEFAULT_PAGE_COUNT*Integer.parseInt(currentPage);
			//if (!Util.isNull(name)) {
				//name = new String(name.getBytes("ISO-8859-1"), "utf-8");
			//}
			
			List<Map<String, Object>> listMap=new ArrayList<>();
			Map<String,Object> addMap=null;
			Date currentDate=DateTimeUtil.stringToDate(DateTimeUtil.getTimeByStyle("yyyy-MM-dd")); 
			int currentHour=Integer.parseInt(DateTimeUtil.getTimeByStyle("HH"));
		    List<Map<String,Object>> list = apiT_goods_topicService.getGoodsInfoByTopicId(topicId,name, from, to);
		    for (Map<String, Object> goodInfo : list) {
		    	int startHour=Integer.parseInt(goodInfo.get("START_TIME").toString().substring(0, 2)) ;
		    	int endHour=Integer.parseInt(goodInfo.get("END_TIME").toString().substring(0, 2));
		    	Date startDate=DateTimeUtil.stringToDate(goodInfo.get("START_DATE").toString());
		    	int diff=DateTimeUtil.compareTwoDate(currentDate,startDate);
		    	if ((diff==0&&currentHour>=startHour&&currentHour<endHour)||(diff==-1||(diff==0&&currentHour<startHour))) {
					addMap=new HashMap<>();
					addMap.put("C_ICO", goodInfo.get("C_ICO"));
					addMap.put("MKT_PRICE", goodInfo.get("MKT_PRICE"));
					addMap.put("PRICE", goodInfo.get("PRICE"));
					addMap.put("NAME", goodInfo.get("NAME"));
					addMap.put("COUNT", Integer.parseInt(goodInfo.get("COUNT").toString()));
					addMap.put("DESCRIPTION", goodInfo.get("DESCRIPTION"));
					addMap.put("TOPIC_LINK_ID",Integer.parseInt(goodInfo.get("TOPIC_LINK_ID").toString()));
					addMap.put("PROPERTY", goodInfo.get("PROPERTY"));
					addMap.put("ID",Integer.parseInt(goodInfo.get("ID").toString()));
					addMap.put("IMAGE", goodInfo.get("IMAGE"));
					addMap.put("STORE", Integer.parseInt(goodInfo.get("STORE").toString()));
					addMap.put("TOPIC_ID", Integer.parseInt(goodInfo.get("TOPIC_ID").toString()));
					listMap.add(addMap);
				}
			}
            map.put("datas", listMap);
            if (!Util.isNull(topicId)) {
            	  ApiT_goods_topic goods_topic= apiT_goods_topicService.getTopicByTopicId(topicId);
                  map.put("startTime", goods_topic.getStart_time());
                  map.put("endTime", goods_topic.getEnd_time());
                  map.put("startDate", goods_topic.getStart_date());
			}else {
				 map.put("startTime", "");
                 map.put("endTime","");
                 map.put("startDate","");
			}
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
}


